package com.zzptc.syl.traffic;

import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity  {
    private static final String TAG = "MainActivity";
    private Spinner spinner;
    private ListView table;
    private List<Lamp.DataBean> data = new ArrayList<>();
    private MyAdapter myAdapter;
    private Button bt_query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
//        getInfo();
    }

    private void initview() {
        table=findViewById(R.id.table);
        spinner = findViewById(R.id.list_srot);
        bt_query=findViewById(R.id.bt_query);
        myAdapter = new MyAdapter(data);
        bt_query.setOnClickListener(view -> myAdapter.notifyDataSetChanged());
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if(data!=null){
                    switch ((int) l){
                        case 0:
                            data.sort((t0, t1) -> t0.getRoad()-t1.getRoad());
                            break;
                        case 1:
                            data.sort((dataBean, t1) -> t1.getRoad()-dataBean.getRoad());
                            break;
                        case 2:
                            data.sort((dataBean, t1) -> dataBean.getRed()-t1.getRed());
                            break;
                        case 3:
                            data.sort(((dataBean, t1) -> t1.getRed()-dataBean.getRed()));
                            break;
                        case 4:
                            data.sort((dataBean, t1) -> dataBean.getYellow()-t1.getYellow());
                            break;
                        case 5:
                            data.sort((dataBean, t1) -> t1.getYellow()-dataBean.getYellow());
                            break;
                        case 6:
                            data.sort((dataBean, t1) -> dataBean.getGreen()-t1.getGreen());
                            break;
                        case 7:
                            data.sort((dataBean, t1) -> t1.getGreen()-dataBean.getGreen());
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
//
//    private void getInfo() {
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl("http://hh1.me:5001/")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//        Service service = retrofit.create(Service.class);
//        Call<Lamp> all = service.all();
//        all.enqueue(new Callback<Lamp>() {
//            @Override
//            public void onResponse(Call<Lamp> call, Response<Lamp> response) {
//                data.clear();
//                data.addAll(response.body().getData());
//                table.setAdapter(myAdapter);
//            }
//
//            @Override
//            public void onFailure(Call<Lamp> call, Throwable t) {
//                Log.d(TAG, "onFailure: " + t.getMessage());
//            }
//        });
//
//    }
}